package com.chaoyong.higo.activity;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class ModifyPayPwd02Activity extends BaseActivity {

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字
	
	private EditText phone;
	private Button submit;
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.base_left_iv:
			finish();
			break;
		case R.id.modify_phone_submit:
			String no = phone.getText().toString();
			if (TextUtils.isEmpty(no)) {
				showToast("请输入新的支付密码");
				return;
			}
			notifyPhone(no);
			break;
		default:
			break;
		}
	}

	@Override
	public int layoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_modify_phone_no_safe;
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub

		
		base_left_iv = (LinearLayout) findViewById(R.id.base_left_iv);
		base_title_tv = (TextView) findViewById(R.id.base_title_tv);
		base_right_tv = (TextView) findViewById(R.id.base_right_tv);

		base_title_tv.setText("修改支付密码");
		base_right_tv.setVisibility(View.GONE);
		base_left_iv.setOnClickListener(this);
		
		phone = (EditText) findViewById(R.id.modify_phone_no);
		phone.setHint("请输入新的支付密码");
		phone.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
		submit = (Button) findViewById(R.id.modify_phone_submit);
		submit.setOnClickListener(this);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	/**
	 * 修改手机号
	 * 
	 * @param oldPone
	 * @param code
	 * @param uid
	 * @param newPhone
	 */
	private void notifyPhone(String newphone) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			jsonObject.putOpt("uid", PrefUtils.getString(this, "userId", ""));
			jsonObject.putOpt("password", phone.getText().toString());
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_savePay, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				try {
					JSONObject o = new JSONObject(arg0.result);
					String data = o.getString("data");
					showToast(data);
					if(o.getInt("status") == 1){
						startActivity(new Intent(ModifyPayPwd02Activity.this, AccountSafeActivity.class));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		});
	}
	
}
